package fr.emse.tatiana.analysis.annotation.actions;



import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.Window;

import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;

import fr.emse.tatiana.CorpusNavigationView;
import fr.emse.tatiana.analysis.annotation.AnnotationAnalysisInput;
import fr.emse.tatiana.commands.ICommandIds;
import fr.emse.tatiana.corpus.Corpus;
import fr.emse.tatiana.replayable.ReplayableInput;


/**
 * Action to create a new annotation-based analysis
 * 
 * @author dyke
 *
 */
public class NewAnnotationAnalysisAction extends Action {

	private final IWorkbenchWindow window;
	private final String navigationID;
	private final String editorID;
	
	private static NewAnnotationAnalysisAction instance;
	
	public static NewAnnotationAnalysisAction getInstance() {
		return instance;
	}

	public NewAnnotationAnalysisAction(IWorkbenchWindow window, String label, String edid, String navid) {
		this.window = window;
		this.navigationID = navid;
		this.editorID = edid;
		setText(label);
		// The id is used to refer to the action in a menu or toolbar
		setId(ICommandIds.CMD_NEW_ANNOTATION_ANALYSIS);
		// Associate the action with a pre-defined command, to allow key bindings.
		//setActionDefinitionId(ICommandIds.CMD_NEW_ANNOTATION_ANALYSIS);
		setImageDescriptor(fr.emse.tatiana.Activator.getImageDescriptor("/icons/sample2.gif"));
		this.setEnabled(false);
		instance = this;
	}


	public void run() {
		if(window != null) {	
			IProject p = ((CorpusNavigationView) window.getActivePage().findView(this.navigationID)).getSelectedCorpus().getProject();
			IEditorInput input = new AnnotationAnalysisInput(p);
			try {
				window.getActivePage().openEditor(input, editorID);
			} catch (PartInitException e) {
				new MessageDialog(window.getShell(), 
						"Problem Opening Analysis",
						null,
						"Could not open analysis " + input.getName(),
						MessageDialog.ERROR,
						new String[]{"Ok"},
						0).open();
			}
		}
	}

}
